from enum import unique, Enum


@unique
class WordType(Enum):
    KEYWORD = 0
    OPERATOR = 1
    ID = 2
    CONST_INT = 3
    SEPARATE = 4
    SPECIAL = 5


WT = WordType
ID = -1


class Word:
    def __init__(self, wt: WordType, name: str):
        global ID
        ID += 1
        self.ID = ID
        self.type = wt
        self.name = name
        self.len = len(name)


wordmap = [
    Word(WT.KEYWORD, "if"),
    Word(WT.KEYWORD, "elif"),
    Word(WT.KEYWORD, "else"),
    Word(WT.OPERATOR, "+="),
    Word(WT.OPERATOR, "-="),
    Word(WT.OPERATOR, "*="),
    Word(WT.OPERATOR, "/="),
    Word(WT.OPERATOR, "+"),
    Word(WT.OPERATOR, "-"),
    Word(WT.OPERATOR, "*"),
    Word(WT.OPERATOR, "/"),
    Word(WT.OPERATOR, ">"),
    Word(WT.OPERATOR, "<"),
    Word(WT.OPERATOR, "<="),
    Word(WT.OPERATOR, ">="),
    Word(WT.OPERATOR, "=="),
    Word(WT.OPERATOR, "!="),
    Word(WT.OPERATOR, "="),
    Word(WT.SEPARATE, ","),
    Word(WT.SEPARATE, "("),
    Word(WT.SEPARATE, ")"),
    Word(WT.SEPARATE, "{"),
    Word(WT.SEPARATE, "}"),
    Word(WT.SPECIAL, " "),
    Word(WT.SPECIAL, "\t"),
    Word(WT.SPECIAL, "\n"),
]
exWordMap = wordmap[:]
exWordMap.extend([Word(WT.CONST_INT, str(i)) for i in range(10)])


def Bulid(n: str):
    n += " "
    trueptr: int = 0
    for ptr in range(len(n)):
        if ptr < trueptr:
            continue
        for i in wordmap:
            if ptr > len(n) - 3:
                continue
            if n[ptr:ptr + i.len] == i.name:
                if i.name in [" ", "\t", "\n"]:
                    trueptr += 1
                    break
                print("检测到", i.type, ":", i.name)
                trueptr += i.len
                break
        else:
            if True:
                try:
                    temp = 0
                    digits = 0
                    int(n[ptr])
                    for i in range(ptr, len(n)):
                        try:
                            int(n[i])
                            temp = temp * 10 + int(n[i])
                            digits += 1
                        except:
                            print("检测到", WT.CONST_INT, ":", temp)
                            trueptr += digits
                            break

                except ValueError:
                    if ptr > len(n) - 3:
                        continue
                    mn = 1E100
                    for i in exWordMap:
                        if n.count(i.name) != 0 and mn > n.index(i.name) - ptr and n.index(i.name) > ptr:
                            mn = n.index(i.name) - ptr
                    print("检测到", WT.ID, ":", n[ptr:mn + ptr])
                    trueptr = ptr + mn

